<template>
    <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './resize'

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '200px'
        },
        autoResize: {
            type: Boolean,
            default: true
        },
        chartData: {
            type: Object,
            // required: true,
        },
        num: {
            type: String,
            default: 'month'
        },
    },
    data() {
        return {
            chart: null
        }
    },
    watch: {
        chartData: {
            deep: true,
            handler(val) {
                // this.setOptions(val)
            }
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    methods: {
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            // this.setOptions(this.chartData)
            this.setOptions()
        },
        setOptions() {
            this.chart.setOption({
                xAxis: {
                    data: ['站点1', '站点2', '站点3', '站点4', '站点5', '站点6'],
                    boundaryGap: false,
                    axisTick: {
                        show: false
                    }
                },
                grid: {
                    left: 10,
                    right: 10,
                    bottom: 20,
                    top: 30,
                    containLabel: true
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'cross'
                    },
                    padding: [5, 10]
                },
                yAxis: {
                    axisTick: {
                        show: false
                    }
                },
                // legend: {
                //     data: ['进水量', '出水量']
                // },
                series: [{
                    name: '进水量',
                    itemStyle: {
                        color: {
                            type: 'linear',
                            x: 0,
                            y: 0,
                            x2: 0,
                            y2: 1,
                            colorStops: [
                                { offset: 0, color: 'rgba(49, 251, 251, 1)' }, // 0% 处的颜色
                                { offset: 1, color: 'rgba(49, 251, 251, 0)' } // 100% 处的颜色
                            ],
                            global: false // 缺省为 false
                        },
                    },
                    smooth: true,
                    type: 'line',
                    areaStyle: {},
                    data: [0, 0, 0, 0, 0, 0, 0],
                    animationDuration: 2800,
                    animationEasing: 'cubicInOut'
                },
                {
                    name: '出水量',
                    smooth: true,
                    type: 'line',
                    itemStyle: {
                        color: {
                            type: 'linear',
                            x: 0,
                            y: 0,
                            x2: 0,
                            y2: 1,
                            colorStops: [
                                { offset: 0, color: 'rgba(6, 93, 255, 0.40)' }, // 0% 处的颜色
                                { offset: 1, color: 'rgba(6, 93, 255, 0)' } // 100% 处的颜色
                            ],
                            global: false // 缺省为 false
                        },
                    },
                    areaStyle: {},
                    data: [0, 0, 0, 0, 0, 0, 0],
                    animationDuration: 2800,
                    animationEasing: 'quadraticOut'
                }]
            })
        }
    }
}
</script>
